﻿// See https://aka.ms/new-console-template for more information

new Test().C();
class Test
{
    static object lock_obj=new object();
    public static List<string> list = new List<string>();
    public void C() {
       var a= A();
        var b=B();
        Task.WaitAll(a,b);
        Console.WriteLine("C");
        Console.WriteLine(list.Count);
    }
    public Task A() {
        return Task.Run(() => {
            //Task.Delay(5000).Wait();
            for (int i = 0; i < 1000; i++) {
                lock (lock_obj)
                {
                    list.Add("A" + i);
                }
            }
            Console.WriteLine("A");
        });        
    }
    public Task B()
    {
        return Task.Run(() => {
            //Task.Delay(2000).Wait();
            for (int i = 0; i < 1000; i++)
            {
                lock (lock_obj)
                {
                    list.Add("B" + i);
                }
            }
            Console.WriteLine("B");
        });
    }
}
